home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.04 Apr 91 / X window Code next >
Encoding:
Text File  |  1991-02-04  |  4.8 KB  |  203 lines  |  [TEXT/nX^n]

  1. Listing 1, "Hello World" using XLIB:
  2.  
  3. #include        <stdio.h>
  4. #include        <X11/Xlib.h>
  5. #include        <X11/Xutil.h>
  6.  
  7. Display * mydisplay;
  8. Window mywindow;
  9. GC mygc;
  10. XEvent myevent;
  11. KeySym mykey;
  12. XSizeHints myhint;
  13. int     myscreen;
  14. unsigned long   myforeground,
  15.                 mybackground;
  16. XFontStruct * font_struct;
  17.  
  18. /* declarations */
  19. char    window_title[] = {"Hello World"};
  20.  
  21. main (argc, argv)
  22. int     argc;
  23. char  **argv;
  24. {
  25.     int     i;
  26.     char    text[10];
  27.     int     done;
  28.     XColor  greencolor;
  29.     XColor  rgb;
  30.  
  31.     mydisplay = XOpenDisplay ("");
  32.     myscreen = DefaultScreen (mydisplay);
  33.  
  34.     /* default pixel values */
  35.     mybackground = WhitePixel (mydisplay, myscreen);
  36.     myforeground = BlackPixel (mydisplay, myscreen);
  37.  
  38.     /* default program-specified window position and size */
  39.     myhint.x = 10;
  40.     myhint.y = 10;
  41.     myhint.width = 250;
  42.     myhint.height = 150;
  43.     myhint.flags = PPosition | PSize;
  44.  
  45.     /* window creation */
  46.     mywindow = XCreateSimpleWindow (mydisplay,
  47.         DefaultRootWindow (mydisplay),
  48.         myhint.x, myhint.y, myhint.width,
  49.             myhint.height,
  50.         5, myforeground, mybackground);
  51.     XSetStandardProperties (mydisplay, mywindow, window_title, window_title, None, argv, argc, &myhint);
  52.  
  53.     /* GC creation and initialization */
  54.     mygc = XCreateGC (mydisplay, mywindow, 0, 0);
  55.     XSetBackground (mydisplay, mygc, mybackground);
  56.     XSetForeground (mydisplay, mygc, myforeground);
  57.     XAllocNamedColor(mydisplay, DefaultColormap(mydisplay, myscreen),
  58.       "MediumForestGreen", &greencolor, &rgb);
  59.  
  60.     /* font initialization */
  61.     font_struct = XLoadQueryFont (mydisplay, "*courier-bold-r-normal--14*");
  62.     XSetFont (mydisplay, mygc, font_struct -> fid);
  63.  
  64.     /* input event selection */
  65.     XSelectInput (mydisplay, mywindow,
  66.         ButtonPressMask | KeyPressMask | ExposureMask);
  67.  
  68.     /* window mapping */
  69.     XMapRaised (mydisplay, mywindow);
  70.     XFlush (mydisplay);
  71.  
  72.     /* main event-reading loop */
  73.     done = 0;
  74.     while (done == 0) {
  75.  
  76.         /* read the next event */
  77.         XNextEvent (mydisplay, &myevent);
  78.         switch (myevent.type) {
  79.  
  80.         /* repaint window on expose events */
  81.         case Expose: 
  82.             XSetForeground(mydisplay, mygc, myforeground);
  83.             XDrawString (mydisplay, mywindow, mygc, 10, 60, "Hello, World", 12);
  84.             XSetForeground(mydisplay, mygc, greencolor);
  85.             XDrawRectangle(mydisplay, mywindow, mygc, 5, 47, 150, 15);
  86.             if (myevent.xexpose.count == 0)
  87.                 XFlush (mydisplay);
  88.             break;
  89.  
  90.         /* process mouse-button presses */
  91.         case ButtonPress: 
  92.             done = 1;
  93.             break;
  94.  
  95.         /* process keyboard input */
  96.         case KeyPress: 
  97.             i = XLookupString (&myevent, text, 10, &mykey, 0);
  98.             if (i == 1 && text[0] == 'q')
  99.                 done = 1;
  100.             break;
  101.  
  102.         default: 
  103.             break;
  104.  
  105.         }            /* switch (myevent.type) */
  106.     }                /* while (done == 0) */
  107.  
  108.     /* termination */
  109.     XFreeGC (mydisplay, mygc);
  110.     XDestroyWindow (mydisplay, mywindow);
  111.     XCloseDisplay (mydisplay);
  112.     exit (0);
  113. }
  114.  
  115. Listing 2, C program for "Hello World" using Motif:
  116.  
  117. #include <X11/Xlib.h>
  118. #include <Mrm/MrmAppl.h>
  119.  
  120. #define WINDOWNAME "Hello World"
  121. #define MYCLASS "HWclass"
  122.  
  123. static MrmHierarchy hierarchy_id;
  124. static char *uid_file [] = {"uil.uid"};
  125. static MrmType widget_class;
  126. static MRMRegisterArg names_list[] =
  127. {
  128. {"hwpushbuttoncb", (caddr_t)hwpushbuttoncb },
  129. {0, 0}
  130. };
  131.  
  132. Widget shellwidget;
  133. Widget bulletinBoard0;
  134.  
  135. /* Callback routine for the pushbutton */
  136. void hwpushbuttoncb(w, client, call)
  137. Widget w;
  138. int client;
  139. XmAnyCallbackStruct *call;
  140. {
  141.     exit(0);
  142. }
  143.  
  144. /* MAIN PROGRAM */
  145. main(argc, argv)
  146. int argc;
  147. char **argv;
  148. {
  149.      Display *display;
  150.  
  151.     shellwidget = XtInitialize(argv[0], "HelloWorld",NULL,0,&argc,argv);
  152.     MrmInitialize();
  153.     MrmOpenHierarchy((MrmCount)(1),uid_file,0, &hierarchy_id);
  154.     MrmRegisterNames(names_list,
  155.         (sizeof(names_list)/sizeof(MRMRegisterArg))-1);
  156.     MrmFetchWidget(hierarchy_id, "bulletinBoard0",shellwidget,
  157.         &bulletinBoard0, &widget_class);
  158.     MrmCloseHierarchy(hierarchy_id);
  159.     XtManageChild(bulletinBoard0);
  160.     XtRealizeWidget(shellwidget);
  161.     XtMainLoop();
  162. }
  163.  
  164. Listing 3, UIL code for "Hello World" using Motif:
  165.  
  166. module helloworld
  167. version = 'V1.0'
  168. names = case_sensitive
  169.  
  170. include file 'XmAppl.uil'; 
  171.  
  172. procedure hwpushbuttoncb();
  173.  
  174. object bulletinBoard0 : XmBulletinBoard widget {
  175.     arguments {
  176.         XmNborderWidth = 0; 
  177.         XmNmarginWidth = 1; 
  178.         XmNmarginHeight = 1; 
  179.         XmNresizePolicy = XmRESIZE_NONE; 
  180.         XmNx = 0; 
  181.         XmNy = 0; 
  182.         XmNwidth = 152; 
  183.         XmNheight = 96; 
  184.     };
  185.     controls {
  186.        XmPushButton pushButton5 ;
  187.     };
  188. };
  189. object pushButton5 : XmPushButton widget {
  190.     arguments {
  191.         XmNbackground = color('MediumForestGreen'); 
  192.         XmNfontList = font('*helvetica-bold-r-normal--14*'); 
  193.         XmNlabelString = 'Hello, World'; 
  194.         XmNx = 18; 
  195.         XmNy = 18; 
  196.     };
  197.     callbacks {
  198.         XmNactivateCallback =  procedure hwpushbuttoncb();
  199.     };
  200. };
  201. end module;
  202.  
  203.